import { Component, OnInit, ViewEncapsulation, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PiciServiceProxy,
    ComboboxItemDto,
    TaskServiceProxy,
    SimpleTaskDto,
    BatchCostServiceProxy,
    SimpleCostListDto,
    CostDetailListDto
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/api';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';

@Component({
    templateUrl: './pici-query.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class PiciQueryComponent extends AppComponentBase implements OnInit {
    constructor(
        injector: Injector,
        private _piciService: PiciServiceProxy,
        private _taskService: TaskServiceProxy,
        private _costService: BatchCostServiceProxy
    ) {
        super(injector);
    }
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('detailPaginator') detailPaginator: Paginator;

    filterText = '';

    picis = new Array<ComboboxItemDto>();
    selectPiciId: number | undefined = undefined;

    selectedTask: SimpleTaskDto;

    costs = new Array<SimpleCostListDto>();
    taskCosts = new Array<SimpleCostListDto>();

    detailTableIsLoading = false;
    detailCostsCount = 0;
    detailCosts: Array<CostDetailListDto>;

    ngOnInit(): void {
        this._piciService.getPiciCombobox().subscribe(result => {
            this.picis = result.items;
        });
    }

    startQuery() {
        this.initDatas();
        this.getTasks();
        this.getCostDetail();
    }

    getTasks(event?: LazyLoadEvent) {
        if (!this.selectPiciId) {
            return;
        }

        this.primengTableHelper.showLoadingIndicator();
        this._taskService
            .getTaskByPici(
            this.filterText,
            this.selectPiciId,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });

        this._costService.getSimpleCostsByPici(this.selectPiciId).subscribe(result => {
            this.costs = result.items;
        });
    }


    searchTasks() {
        this.getTasks();
    }

    getCostDetail(event?: LazyLoadEvent) {
        if (!this.selectPiciId) {
            return;
        }

        this.detailTableIsLoading = true;
        this._costService
            .getCostDetailsByPici(
            this.selectPiciId,
            null,
            this.primengTableHelper.getMaxResultCount(this.detailPaginator, event),
            this.primengTableHelper.getSkipCount(this.detailPaginator, event)
            )
            .subscribe(result => {
                this.detailCostsCount = result.totalCount;
                this.detailCosts = result.items;
                this.detailTableIsLoading = false;
            });
    }

    onTaskSelect(event) {
        if (!this.selectedTask.id) {
            return;
        }
        this._costService.getSimpleCostsByTask(this.selectedTask.id).subscribe(result => {
            this.taskCosts = result.items;
        });

        this._costService.getCostDetailsByTask(
            this.selectedTask.id,
            null,
            this.primengTableHelper.getMaxResultCount(this.detailPaginator, event),
            this.primengTableHelper.getSkipCount(this.detailPaginator, event))
            .subscribe(result => {
                this.detailCostsCount = result.totalCount;
                this.detailCosts = result.items;
        });
    }

    private initDatas() {
        this.selectedTask = undefined;
        this.costs = new Array<SimpleCostListDto>();
        this.taskCosts = new Array<SimpleCostListDto>();

        this.primengTableHelper.totalRecordsCount = 0;
        this.primengTableHelper.records = undefined;
        this.detailCosts = undefined;
        this.detailCostsCount = 0;
    }
}
